package com.sxfq.loansupermarket.backsystem.modular.logsf.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.common.constant.factory.PageFactory;
import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;
import com.sxfq.loansupermarket.backsystem.modular.logsf.entity.*;
import com.sxfq.loansupermarket.backsystem.modular.logsf.service.IReservationsLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author py
 * @Date 2019年4月19日23:15:54
 */
@Controller
@RequestMapping("/reservationsLog")
public class ReservationsLogController extends BaseController {
    @Autowired
    IReservationsLogService service;

    private String PREFIX = "/logsf/reservationsLog/";

    @RequestMapping(value = "/delete")
    public String delete(Model model) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        return PREFIX + "reservations_delete_log.html";
    }

    @RequestMapping(value = "/update")
    public String update(Model model) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        return PREFIX + "reservations_update_log.html";
    }

    @RequestMapping(value = "/updatefollower")
    public String updatefollower(Model model) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        return PREFIX + "reservations_updatefollower_log.html";
    }

    /**
     * 取消预约患者
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/cance")
    public String cance(Model model) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        return PREFIX + "reservations_cance_log.html";
    }

    @RequestMapping(value = "/updateTreatmentUnitl")
    public String updateTreatmentUnitl(Model model) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        model.addAttribute("outpatientManagementList", ShiroKit.getSessionAttr("outpatientManagementList"));
        return PREFIX + "update_ureatment_unitl.html";
    }

    /**
     * 科室审核列表加载
     */
    @RequestMapping("/updateTreatmentUnitlList")
    @ResponseBody
    public Object updateTreatmentUnitlList(SearchTreatmentUnitlLogInfo info, HttpServletRequest request) {
        Page<TreatmentUnitlLogInfo> page = new PageFactory<TreatmentUnitlLogInfo>().defaultPage();
        try {
            info.setStartIndex(page.getSize() * (page.getCurrent() - 1));
            info.setLimitIndex(page.getSize());
            page.setCurrent(0);
            List<TreatmentUnitlLogInfo> list = service.getDeleteListks(page, info);
            if (list.size() == 0) {
                info.setStartIndex(0);
                list = service.getDeleteListks(page, info);
            }
            page.setRecords(list);
            page.setTotal(service.getQueryPagesCountks(page, info));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }

    /**
     * 市场预约患者删除日志列表
     */
    @RequestMapping("/deleteList")
    @ResponseBody
    public Object deleteList(SearchReservationsDeleteLogInfo info, HttpServletRequest request) {
        Page<ReservationsDeleteLogInfo> page = new PageFactory<ReservationsDeleteLogInfo>().defaultPage();
        try {
            info.setStartIndex(page.getSize() * (page.getCurrent() - 1));
            info.setLimitIndex(page.getSize());
            page.setCurrent(0);
            List<ReservationsDeleteLogInfo> list = service.getDeleteList(page, info);
            if (list.size() == 0) {
                info.setStartIndex(0);
                list = service.getDeleteList(page, info);
            }
            page.setRecords(list);
            page.setTotal(service.getQueryPagesCount(page, info));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }

    /**
     *
     */
    @RequestMapping("/canceList")
    @ResponseBody
    public Object canceList(SearchReservationsCanceLogInfo info, HttpServletRequest request) {
        Page<ReservationsCanceLogInfo> page = new PageFactory<ReservationsCanceLogInfo>().defaultPage();
        try {
            info.setStartIndex(page.getSize() * (page.getCurrent() - 1));
            info.setLimitIndex(page.getSize());
            page.setCurrent(0);
            List<ReservationsCanceLogInfo> list = service.getCanceList(page, info);
            if (list.size() == 0) {
                info.setStartIndex(0);
                list = service.getCanceList(page, info);
            }
            page.setRecords(list);
            page.setTotal(service.getQueryPagesCounts(page, info));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }

    @RequestMapping("/updateList")
    @ResponseBody
    public Object updateList(SearchReservationsUpdateLogInfo info, HttpServletRequest request) {
        Page<ReservationsUpdateLogInfo> page = new PageFactory<ReservationsUpdateLogInfo>().defaultPage();
        try {
            info.setStartIndex(page.getSize() * (page.getCurrent() - 1));
            info.setLimitIndex(page.getSize());
            page.setCurrent(0);
            List<ReservationsUpdateLogInfo> list = service.getUpdateList(page, info);
            if (list.size() == 0) {
                info.setStartIndex(0);
                list = service.getUpdateList(page, info);
            }
            page.setRecords(list);
            page.setTotal(service.getQueryPagesCountt(page, info));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }

    @RequestMapping("/updateFollowerList")
    @ResponseBody
    public Object updateFollowerList(SearchReservationsUpdateLogInfo info, HttpServletRequest request) {
        Page<ReservationsUpdateLogInfo> page = new PageFactory<ReservationsUpdateLogInfo>().defaultPage();
        try {
            info.setStartIndex(page.getSize() * (page.getCurrent() - 1));
            info.setLimitIndex(page.getSize());
            page.setCurrent(0);
            List<ReservationsUpdateLogInfo> list = service.getUpdatefollowerList(page, info);
            if (list.size() == 0) {
                info.setStartIndex(0);
                list = service.getUpdatefollowerList(page, info);
            }
            page.setRecords(list);
            page.setTotal(service.getQueryPagesfollowerCountt(page, info));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }
}